package com.yc.sole.system.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import com.yc.sole.system.dto.LogDto;
import com.yc.sole.system.entity.Log;
import com.yc.sole.system.mapper.LogMapper;
import com.yc.sole.system.request.LogQueryReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2023-04-01
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class LogRepository extends ServiceImpl<LogMapper, Log> {

    /**
     * 新增
     *
     * @param logDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void saveLog(LogDto logDto) {
        Log entity = new Log();
        BeanUtil.copyProperties(logDto, entity);
        this.save(entity);
    }

    /**
     * 通过id删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteLog(List<Long> ids) {
        this.removeByIds(ids);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public LogDto selectLogById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), LogDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<LogDto> selectLogPage(Paging page, LogQueryReq logQueryReq) {
        LambdaQueryWrapper<Log> queryWrapper = Wrappers.<Log>lambdaQuery()
                .select(Log::getId, Log::getUsername,
                        Log::getUserId, Log::getIp, Log::getAddress,
                        Log::getErrorFlag, Log::getExecutionTime,
                        Log::getRequestTime, Log::getUri,
                        Log::getMethodName)
                .like(CharSequenceUtil.isNotBlank(logQueryReq.getUsername()), Log::getUsername, logQueryReq.getUsername())
                .like(CharSequenceUtil.isNotBlank(logQueryReq.getUri()), Log::getUri, logQueryReq.getUri())
                .like(CharSequenceUtil.isNotBlank(logQueryReq.getMethodName()), Log::getMethodName, logQueryReq.getMethodName())
                .like(CharSequenceUtil.isNotBlank(logQueryReq.getIp()), Log::getIp, logQueryReq.getIp())
                .eq(Objects.nonNull(logQueryReq.getErrorFlag()), Log::getErrorFlag, logQueryReq.getErrorFlag())
                .orderByDesc(Log::getId);
        if (CollUtil.isNotEmpty(logQueryReq.getDateRange())
                && CharSequenceUtil.isNotBlank(logQueryReq.getDateRange().get(0))
                && CharSequenceUtil.isNotBlank(logQueryReq.getDateRange().get(1))) {
            queryWrapper.between(Log::getRequestTime, logQueryReq.getDateRange().get(0), logQueryReq.getDateRange().get(1));
        }
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), LogDto.class);
    }
}
